﻿Imports DTO
Imports DAO
Imports BUS


Public Class frmTraCuuSach : Inherits DevComponents.DotNetBar.Office2007Form


    Private Sub cbTheLoaiSach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTheLoaiSach.CheckedChanged
        cboTheLoaiSach.Enabled = cbTheLoaiSach.Checked
    End Sub

    Private Sub cbNhaXuatBan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbNhaXuatBan.CheckedChanged
        txtNhaXuatBan.Enabled = cbNhaXuatBan.Checked
    End Sub

    Private Sub cbNamXuatBan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbNamXuatBan.CheckedChanged
        dtpNamXuatBanDen.Enabled = cbNamXuatBan.Checked
        dtpNamXuatBanTu.Enabled = cbNamXuatBan.Checked
    End Sub

    Private Sub cbMaSach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbMaSach.CheckedChanged
        txtMaSach.Enabled = cbMaSach.Checked
    End Sub

    Private Sub cbTenSach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTenSach.CheckedChanged
        cboMaDauSach.Enabled = cbTenSach.Checked
    End Sub

    Private Sub cbTenTacGia_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTenTacGia.CheckedChanged
        txtTenTacGia.Enabled = cbTenTacGia.Checked
    End Sub



    Private Sub frmTraCuuSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtMaSach.Enabled = False
        txtNhaXuatBan.Enabled = False
        cboMaDauSach.Enabled = False
        txtTenTacGia.Enabled = False
        cboTheLoaiSach.Enabled = False
        dtpNamXuatBanDen.Enabled = False
        dtpNamXuatBanTu.Enabled = False
        cboTinhTrangSach.Enabled = False


        Dim tlsbus As New CTheLoaiSachBUS()
        Dim listtheloaisachdto As List(Of CTheLoaiSachDTO) = tlsbus.LayDanhSachTheLoaiSach()
        For Each theloaisachdto As CTheLoaiSachDTO In listtheloaisachdto
            cboTheLoaiSach.Items.Add(theloaisachdto)
        Next

        cboTheLoaiSach.ValueMember = "Ma"
        cboTheLoaiSach.DisplayMember = "Ten"

        cboTheLoaiSach.SelectedIndex = 1


        Dim ttsachbus As New CTinhTrangSachBUS()
        Dim listtinhtrangsachdto As List(Of CTinhTrangSachDTO) = ttsachbus.LayDanhSachTinhTrangSach()
        For Each ttsachdto As CTinhTrangSachDTO In listtinhtrangsachdto
            cboTinhTrangSach.Items.Add(ttsachdto)
        Next

        cboTinhTrangSach.ValueMember = "Ma"
        cboTinhTrangSach.DisplayMember = "Ten"

        cboTinhTrangSach.SelectedIndex = 1


        Dim dausachbus As New CDauSachBUS()
        Dim listdausachdto As List(Of CDauSachDTO) = dausachbus.LayDanhSachDauSach()
        For Each dausachdto As CDauSachDTO In listdausachdto
            cboMaDauSach.Items.Add(dausachdto)
        Next

        cboMaDauSach.ValueMember = "Ma"
        cboMaDauSach.DisplayMember = "Ten"

        cboMaDauSach.SelectedIndex = 1

    End Sub

    Private stt As Integer = 0

    Private Sub btnTraCuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTraCuu.Click
        stt = 1
        Dim sachbus As New CSachBUS()
        Dim sachcrt As New CSachCrt()
        If (cbMaSach.Checked = True) Then
            If (Not Integer.TryParse(txtMaSach.Text, sachcrt.Ma)) Then
                MessageBox.Show("Mã sách không hợp lệ", "Thong Bao")
                txtMaSach.Focus()
                Return
            End If
        End If
        sachcrt.CheckMaSach = cbMaSach.Checked
        sachcrt.CheckNamXuatBan = cbNamXuatBan.Checked
        sachcrt.CheckNhaXuatBan = cbNhaXuatBan.Checked
        sachcrt.CheckTenSach = cbTenSach.Checked
        sachcrt.CheckTenTacGia = cbTenTacGia.Checked
        sachcrt.CheckTheLoaiSach = cbTheLoaiSach.Checked
        sachcrt.CheckTinhTrangSach = cbTinhTrangSach.Checked

        If (cbTenSach.Checked = True) Then
            Dim dausachdto As CDauSachDTO = cboMaDauSach.SelectedItem
            sachcrt.Ten = dausachdto.Ten
            sachcrt.MaDauSach = dausachdto.Ma
        End If

        If (cbTenTacGia.Checked = True) Then
            sachcrt.TenTacGia = txtTenTacGia.Text
        End If

        If cbTheLoaiSach.Checked = True Then
            Dim theloaidto As CTheLoaiSachDTO = cboTheLoaiSach.SelectedItem
            sachcrt.MaTheLoai = theloaidto.Ma
            sachcrt.TenTheLoai = theloaidto.Ten
        End If

        If (cbNhaXuatBan.Checked = True) Then
            sachcrt.NhaXuatBan = txtNhaXuatBan.Text
        End If

        If cbTinhTrangSach.Checked = True Then
            Dim tinhtrangdto As CTinhTrangSachDTO = cboTinhTrangSach.SelectedItem
            sachcrt.MaTinhTrang = tinhtrangdto.Ma
            sachcrt.TenTinhTrang = tinhtrangdto.Ten
        End If


        If cbNamXuatBan.Checked = True Then
            If (Not DateTime.TryParse(dtpNamXuatBanTu.Value, sachcrt.NamXuatBanTu)) Then
                MessageBox.Show("""Năm xuất bản từ"" chưa hợp lệ!", "Thong Bao")
                dtpNamXuatBanTu.Focus()
                Return
            End If

            If (Not DateTime.TryParse(dtpNamXuatBanDen.Value, sachcrt.NamXuatBanDen)) Then
                MessageBox.Show("""Năm xuất bản đến"" chưa hợp lệ!", "Thong Bao")
                dtpNamXuatBanDen.Focus()
                Return
            End If
        End If

        Dim listsachdto As List(Of CSachDTO)
        If (cbMaSach.Checked = True) Then
            Dim sachdto As CSachDTO = sachbus.TraCuu(sachcrt.Ma)
            dgvDanhSachSach.Rows.Clear()
            If sachdto Is Nothing Then
                Return
            End If
            dgvDanhSachSach.Rows.Add(stt, sachdto.Ma, sachdto.MaDauSach, sachdto.MaTheLoai, sachdto.MaTinhTrang, sachdto.Ten, sachdto.TacGia, sachdto.TenTheLoai, sachdto.NhaXuatBan, sachdto.TenTinhTrang, sachdto.TienPhatMatSach, sachdto.TriGia)
        Else
            listsachdto = sachbus.TraCuuKhongCoMaSach(sachcrt)
            dgvDanhSachSach.Rows.Clear()
            For Each sachdto As CSachDTO In listsachdto
                dgvDanhSachSach.Rows.Add(stt, sachdto.Ma, sachdto.MaDauSach, sachdto.MaTheLoai, sachdto.MaTinhTrang, sachdto.Ten, sachdto.TacGia, sachdto.TenTheLoai, sachdto.NhaXuatBan, sachdto.TenTinhTrang, sachdto.TienPhatMatSach, sachdto.TriGia)
                stt += 1
            Next
        End If

    End Sub


    Private Sub cbTinhTrangSach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTinhTrangSach.CheckedChanged
        cboTinhTrangSach.Enabled = cbTinhTrangSach.Checked
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        txtMaSach.Enabled = False
        txtNhaXuatBan.Enabled = False
        cboMaDauSach.Enabled = False
        txtTenTacGia.Enabled = False
        cboTheLoaiSach.Enabled = False
        dtpNamXuatBanDen.Enabled = False
        dtpNamXuatBanTu.Enabled = False
        cboTinhTrangSach.Enabled = False

        txtMaSach.Text = ""
        txtNhaXuatBan.Text = ""
        txtTenTacGia.Text = ""
        cbMaSach.Checked = False
        cbNamXuatBan.Checked = False
        cbNhaXuatBan.Checked = False
        cbTenSach.Checked = False
        cbTenTacGia.Checked = False
        cbTheLoaiSach.Checked = False
        cbTinhTrangSach.Checked = False
    End Sub

End Class